<template>
  <div>
    <div slot="header" class="clearfix">
      <div class="p_title" style="font-size: 20px">
        <el-button type="text" @click="$router.go(-1)"
          ><i
            class="el-icon-back"
            style="font-size: 20px; margin-right: 10px"
          ></i
        ></el-button>
        <span class="p_title">{{ title }}</span>
      </div>
    </div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="商品描述" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input
          placeholder="请输入商品价格"
          type="number"
          v-model="ruleForm.price"
        >
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="起始库存" prop="price">
        <el-input
          placeholder="请输入起始库存"
          type="number"
          v-model="ruleForm.inventory"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="商品分类" prop="categoryIds">
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品规格" prop="categoryIds"></el-form-item>
      <el-form-item label="商品图片">
        <upload-picture
          @getimgArr="
            (imgArr) => {
              this.imgArr = imgArr;
            }
          "
          :imgArr="imgArr"
        ></upload-picture>
      </el-form-item>

      <el-form-item label="商品详情" prop="detail">
        <quill-editor
          class="ql-editor"
          v-model="ruleForm.detail"
          
        >
        </quill-editor>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {addProduct} from "../api/index"
export default {
  data() {
    var checkPrice = (rule, value, callback) => {
      if (value * 1 < 0) {
        return callback(new Error("商品单价不能小于0"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        categoryIds: [],
        desc: "",
        price: "",
        detail: "",
        specAttr: [],
        inventory: 0, // 起始库存
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入商品名称名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        desc: [
          {
            required: true,
            message: "请输入商品描述",
            trigger: "blur",
          },
          {
            min: 5,
            max: 100,
            message: "长度在 5 到 100 个字符",
            trigger: "blur",
          },
        ],
        price: [
          {
            required: true,
            message: "请输入商品价格",
            trigger: "blur",
          },
          {
            validator: checkPrice,
            trigger: "blur",
          },
        ],
        categoryIds: [
          {
            required: true,
            message: "请选择商品分类",
            trigger: "blur",
          },
        ],
      },
      title: "添加商品",
    };
  },
  created(){
    if(this.$route.params.title)
          this.title=this.$route.params.title
  },
  methods:{
      async addProduct(ruleForm){
          await addProduct(ruleForm)
      },
      submitForm(formName){
          this.$refs[formName].validate((valid) => {
          if (valid) {
            this.addProduct(formName)
            
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName){
          this.$refs[formName].resetFields();
      }
  }
};
</script>
<style scoped>
</style>